/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.facets;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import unc.pds.filemodel.AlbumRemote;
import unc.pds.filemodel.FileRemote;
import unc.pds.filemodel.FolderRemote;
import unc.pds.filemodel.PhotoRemote;
import unc.pds.model.DiaryRemote;
import unc.pds.model.GroupRemote;
import unc.pds.model.Model;
import unc.pds.model.UserRemote;
import unc.pds.news.News;
import unc.pds.service.ServiceBeanRemote;

/**
 *
 * @author Mikhail
 */
public class NewsDrawer {
    public static String drawNews(ArrayList news, ServiceBeanRemote service, int maxNumber) throws RemoteException {
        String result = "";
        Iterator iter = news.iterator();
        int i = 0;
        while (iter.hasNext()) {
            News current = (News)iter.next();
            Model context = (Model)service.getByKey(current.context);
            result += "<p>";
            if (context instanceof UserRemote) {
                result += "Пользователь "+((UserRemote)context).getNickname();
            } else if (context instanceof GroupRemote) {
                result += "Группа "+((GroupRemote)context).getName();
            }
            result += " - ";
            if (current.event.equals("create")) {
                result += "Добавлено";
            } else {
                result += "Неизвестное событие";
            }
            result += " - ";
            Model entity = (Model)service.getByKey(current.entityID);
            if (entity instanceof DiaryRemote) {
                DiaryRemote diary = (DiaryRemote)entity;
                result += "Запись дневника <a href=\"index.jsp?id="+diary.getKey()+"\"/>"+diary.getCaption()+"</a>";
            } else if (entity instanceof FileRemote) {
                FileRemote file = (FileRemote)entity;
                result += "Файл <a href=\"index.jsp?id="+file.getKey()+"\"/>"+file.getName()+"</a>";
            } else if (entity instanceof PhotoRemote) {
                PhotoRemote photo = (PhotoRemote)entity;
                result += "Фотография <a href=\"index.jsp?id="+photo.getKey()+"\"/>"+photo.getName()+"</a>";
            } else if (entity instanceof AlbumRemote) {
                AlbumRemote album = (AlbumRemote)entity;
                result += "Альбом <a href=\"index.jsp?id="+album.getKey()+"\"/>"+album.getName()+"</a>";
            } else if (entity instanceof FolderRemote) {
                FolderRemote folder = (FolderRemote)entity;
                result += "Папка <a href=\"index.jsp?id="+folder.getKey()+"\"/>"+folder.getName()+"</a>";
            } else {
                result += "<a href=\"index.jsp&id="+entity.getKey()+"\">Объект</a>";
            }
            result += "</p>";
            i++;
            if ((i>=maxNumber)&&(maxNumber!=-1)) break;
        }
        return result;
    }
}
